package com.yida.gwj.config.service.impl;

import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfCalculateData;
import com.yida.gwj.config.mapper.GwjConfCalculateDataMapper;
import com.yida.gwj.config.service.IGwjConfCalculateDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 计算金额算法Service业务层处理
 *
 * @author lqq
 * @date 2022-12-13
 */
@Service
public class GwjConfCalculateDataServiceImpl implements IGwjConfCalculateDataService {

	@Autowired
	private GwjConfCalculateDataMapper gcCalculateDataMapper;

	/**
	 * 查询计算金额算法列表
	 *
	 * @param gcCalculateData 计算金额算法
	 * @return 计算金额算法
	 */
	@Override
	public List<GwjConfCalculateData> selectGwjConfCalculateDataList(GwjConfCalculateData gcCalculateData) {
		gcCalculateData.setDelFlag(UserStatus.OK.getCode());
		return gcCalculateDataMapper.selectGwjConfCalculateDataList(gcCalculateData);
	}

	/**
	 * 查询计算标准列表
	 *
	 * @param calculateDataId 计算金额算法主键
	 * @return 计算标准
	 */
	@Override
	public GwjConfCalculateData selectByCalculateDataId(Long calculateDataId) {
		return gcCalculateDataMapper.selectByGwjConfCalculateDataId(calculateDataId);
	}

	/**
	 * 新增计算金额算法
	 *
	 * @param gcCalculateData 计算金额算法
	 * @return 结果
	 */
	@Override
	public int insertGwjConfCalculateData(GwjConfCalculateData gcCalculateData) {
		hasGwjConfCalculateData(gcCalculateData);
		gcCalculateData.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		gcCalculateData.setCreateTime(DateUtils.getNowDate());
		return gcCalculateDataMapper.insertGwjConfCalculateData(gcCalculateData);
	}

	/**
	 * 修改计算金额算法
	 *
	 * @param gcCalculateData 计算金额算法
	 * @return 结果
	 */
	@Override
	public int updateGwjConfCalculateData(GwjConfCalculateData gcCalculateData) {
		hasGwjConfCalculateData(gcCalculateData);
		gcCalculateData.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		gcCalculateData.setUpdateTime(DateUtils.getNowDate());
		return gcCalculateDataMapper.updateGwjConfCalculateData(gcCalculateData);
	}

	/**
	 * 判定当前计算标准是否存在
	 * 根据类别ID，楼栋ID，地区ID，金额判定是否存在标准
	 *
	 * @param gcCalculateData 计算金额算法
	 */
	private void hasGwjConfCalculateData(GwjConfCalculateData gcCalculateData) {
		if (gcCalculateDataMapper.hasGwjConfCalculateData(gcCalculateData) > 1) {
			throw new ServiceException("已存在相同配置计算标准！");
		}
	}

	/**
	 * 批量删除计算金额算法
	 *
	 * @param calculateDataIds 需要删除的计算金额算法主键
	 * @return 结果
	 */
	@Override
	public int deleteByGwjConfCalculateDataIds(Long[] calculateDataIds) {
		List<GwjConfCalculateData> list = new ArrayList<GwjConfCalculateData>();
		for (Long calculateDataId : calculateDataIds) {
			GwjConfCalculateData temp = new GwjConfCalculateData();
			temp.setDelFlag(UserStatus.DELETED.getCode());
			temp.setCalculateDataId(calculateDataId);
			temp.setUpdateTime(DateUtils.getNowDate());
			temp.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
			list.add(temp);
		}
		if (list.size() > 0) {
			return gcCalculateDataMapper.updateBatchByGwjConfCalculateData(list);
		}
		return 0;
	}

	/**
	 * 查询计算标准值数据
	 *
	 * @param calculateTypeId 计算类型ID
	 * @param areaId          地区ID
	 * @param buildTypeId     楼栋类型ID
	 * @return 计算金额算法
	 */
	@Override
	public GwjConfCalculateData selectCalculateDataByCalculateTypeIdAndAreaIdAndBuildTypeId(Long calculateTypeId, Long areaId, Integer buildTypeId) {
		return gcCalculateDataMapper.selectCalculateDataByCalculateTypeIdAndAreaIdAndBuildTypeId(calculateTypeId, areaId, buildTypeId);
	}
}
